﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lite;
using System.Data.SqlClient;
using Csla.Data;
using Csla;
using OpenExpressApp;
using System.Collections;
using Csla.Core;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.MetaModel;
using OpenExpressApp.Types;
using OpenExpressApp.DB.Utils;

namespace OpenExpressApp.Module.Library
{
    [Serializable]
    public partial class PositionList : GBusinessListBase<PositionList, Position>
    {
        protected override string ConnectionString
        {
            get
            {
                return ConnectionStringNames.OpenExpressApp;
            }
        }

        #region  Factory Methods

        public static PositionList GetList()
        {
            return DataPortal.Fetch<PositionList>();
        }


        private PositionList()
        {
            /* require use of factory methods */
            AllowNew = true;
            /* 不允许修改时，AutoUI绑定列表控件时会使得控件只读  */
            //AllowEdit = false;            
        }

        protected override object AddNewCore()
        {
            var item = Position.New();
            Add(item);
            return item;
        }

        #endregion

        #region  Data Access

        private void DataPortal_Fetch()
        {

            RaiseListChangedEvents = false;
            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                IQuery q = db.Query();
                var list = db.Select<Position>(q);
                foreach (var item in list)
                {
                    this.Add(Position.GetLazy(item.Id));
                }
            }
            RaiseListChangedEvents = true;
        }

        #endregion
    }

    [Serializable]
    [Table]
    [DefaultObject("3D6FF74E-6112-4287-8E99-01B18E551117", Catalog = "用户权限", Index = 3, ModuleType = ModuleType.List), Label("岗位管理")]
    public partial class Position : Entity<Position>
    {
        #region  Business Methods

        private static PropertyInfo<string> CodeProperty =
            RegisterProperty(new PropertyInfo<string>("Code"));
        [Column]
        [EntityProperty]
        [Required, ShowInList, ShowInDetail, Label("编码"), ShowInLookup]
        public string Code
        {
            get { return GetProperty(CodeProperty); }
            set { SetProperty(CodeProperty, value); }
        }

        private static PropertyInfo<string> NameProperty =
          RegisterProperty(new PropertyInfo<string>("Name"));
        [Column]
        [EntityProperty]
        [Required, IsTitle, ShowInList, ShowInDetail, Label("名称")]
        public string Name
        {
            get { return GetProperty(NameProperty); }
            set { SetProperty(NameProperty, value); }
        }

        private static PropertyInfo<CycleType> CycleTypeProperty =
          RegisterProperty(new PropertyInfo<CycleType>("CycleType"));
        [Column]
        [EntityProperty, IsReadOnly]
        [Required, ShowInList, ShowInDetail, Label("使用周期")]
        public CycleType CycleType
        {
            get { return GetProperty(CycleTypeProperty); }
            set { SetProperty(CycleTypeProperty, value); }
        }

        private static PropertyInfo<int> LoginTotalCountProperty =
          RegisterProperty(new PropertyInfo<int>("LoginTotalCount"));
        [Column]
        [EntityProperty]
        [Required, ShowInList, ShowInDetail, Label("使用次数")]
        public int LoginTotalCount
        {
            get { return GetProperty(LoginTotalCountProperty); }
            set { SetProperty(LoginTotalCountProperty, value); }
        }

        private static PropertyInfo<double> UseTimeProperty =
          RegisterProperty(new PropertyInfo<double>("UseTime"));
        [Column]
        [Required]
        [EntityProperty]
        public double UseTime
        {
            get { return GetProperty(UseTimeProperty); }
            set { SetProperty(UseTimeProperty, value); }
        }
        [EntityProperty]
        [Required]
        [ShowInList, ShowInDetail, Label("使用分钟(小时/次)")]
        public int UseTimeView
        {
            get
            {
                return (int)(this.UseTime * 60);
            }
            set
            {
                this.UseTime = value / 60d;
            }
        }

        #endregion

        #region  Factory Methods

        private Position() { }

        public static Position Get(Guid id)
        {
            return DataPortal.Fetch<Position>(new SingleLazyCriteria<Position, Guid>(id, false));
        }

        internal static Position GetLazy(Guid id)
        {
            return DataPortal.Fetch<Position>(new SingleLazyCriteria<Position, Guid>(id, true));
        }

        #endregion

        #region  Data Access

        private void DataPortal_Fetch(SingleLazyCriteria<Position, Guid> criteria)
        {
            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                IQuery q = db.Query().Constrain("Id").Equal(criteria.Value);
                var data = db.Select<Position>(q)[0];

                this.Clone(data, false);

                this.IsObjectLazy = criteria.IsObjectLazy;
            }
        }

        #endregion
    }
}